package org.zyzboole.base.encrypt.crypto;

import org.apache.commons.logging.LogFactory;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * 描述:
 *
 * @author zyzboole
 * @date 2023/03/21
 */
public class MD5Kit {

    public static String makeMd5(String paramString) {
        MessageDigest localMessageDigest = null;
        try {
            localMessageDigest = MessageDigest.getInstance("MD5");
            localMessageDigest.reset();
            localMessageDigest.update(paramString.getBytes(StandardCharsets.UTF_8));
        } catch (Exception localException) {
            LogFactory.getLog(MD5Kit.class).error(localException);
            throw new RuntimeException();
        }
        byte[] arrayOfByte = localMessageDigest.digest();
        StringBuffer localStringBuffer = new StringBuffer();
        for (int i = 0; i < arrayOfByte.length; i++) {
            if (Integer.toHexString(0xFF & arrayOfByte[i]).length() == 1) {

            } else {
                localStringBuffer.append(Integer
                        .toHexString(0xFF & arrayOfByte[i]));
            }

        }

        return localStringBuffer.toString();
    }

    public static String makeMd5(String paramString, int paramInt) throws Exception {
        String str = paramString;
        for (int i = 0; i < paramInt; i++) {
            str = makeMd5(str);
        }
        return str;
    }

    /***
     * MD5加码 生成32位md5码
     */
    public static String string2Md5(String inStr) {
        try {
            byte[] btInput = inStr.getBytes(StandardCharsets.UTF_8);
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < md.length; i++) {
                int val = ((int) md[i]) & 0xff;
                if (val < 16) {
                    sb.append("0");
                }
                sb.append(Integer.toHexString(val));
            }
            return sb.toString();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 加密解密算法 执行一次加密，两次解密
     */
    public static String convertMd5(String inStr) {

        char[] a = inStr.toCharArray();
        for (int i = 0; i < a.length; i++) {
            a[i] = (char) (a[i] ^ 't');
        }
        String s = new String(a);
        return s;

    }

    /**
     * 生成随机字符串
     *
     * @param origin
     * @param charsetname
     * @return
     */
    public static String md5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname)) {
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
            }
        } catch (Exception exception) {
        }
        return resultString;
    }

    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return HEX_DIG_ITS[d1] + HEX_DIG_ITS[d2];
    }

    private static final String[] HEX_DIG_ITS = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
}
